<?php

namespace Biz\module\Classes;

use Biz\model\trade\OrderGoods as mOrderGoods;
use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\service\ShopInfo as sShopInfo;

class OrderRefundList
{
    private $where = [];

    public function getList($uid, $page, $pageSize)
    {
        $result = [];

        $skuInfo = [];

        $this->where['uid'] = $uid;

        $count = $this->getCount();

        if (empty($count)) {
            return [];
        }

        $pageList = $this->getPageList($count, $page, $pageSize);

        $this->where['LIMIT'] = [($page - 1) * $pageSize, $pageSize];

        $refundList = $this->getRefundList();

        $shopIds = array_column($refundList, 'shop_id');

        $shopInfos = sShopInfo::getShopInfoList($shopIds);

        $orderNos = array_column($refundList, 'order_no');

        $orderGoods = $this->getOrderGoodsList($orderNos);

        foreach ($refundList as $refund) {
            foreach ($orderGoods as $orderGood) {
                if ($orderGood['sku_id'] == $refund['sku_id'] && $orderGood['goods_id'] == $refund['goods_id']) {
                    $orderGood['cover_img'] = $orderGood['cover_img_thumb_url'];
                    $skuInfo[$refund['refund_order_no']] = $orderGood;
                    continue;
                }
            }
        }


        array_walk($refundList, function ($v, $k) use (&$result, $shopInfos, $skuInfo) {
            $result['list'][$k]['refund_order_no'] = $v['refund_order_no'];
            $result['list'][$k]['refund_amount'] = $v['refund_amount'];
            $result['list'][$k]['refund_integral'] = $v['refund_integral'];
            $result['list'][$k]['refund_rmb'] = $v['refund_rmb'];
            $result['list'][$k]['status'] = $v['v_status'];
            $result['list'][$k]['status_desc'] = $v['v_status_desc'];
            $result['list'][$k]['shop_info'] = [
                'shop_id'   => $v['shop_id'],
                'shop_name' => $shopInfos[$v['shop_id']]['shop_name'],
                'imgurl'    => $shopInfos[$v['shop_id']]['imgurl'],
                'shop_icon' => $shopInfos[$v['shop_id']]['imgurl']
            ];
            $result['list'][$k]['sku_info'] = $skuInfo[$v['refund_order_no']];
        });
        $result['page'] = $pageList;

        return $result;
    }

    public function getCount()
    {
        return mOrderRefund::count('*', $this->where);
    }

    private function getPageList($count, $page, $pageSize)
    {
        $totalPage = ceil($count / $pageSize);
        $prevPage = ($page - 1) <= 0 ? $page : $page - 1;
        $nextPage = ($page + 1) > $totalPage ? $totalPage : $page + 1;

        return [
            'now_page'   => (int)$page,
            'next_page'  => (int)$nextPage,
            'prev_page'  => (int)$prevPage,
            'total_page' => (int)$totalPage
        ];
    }

    public function getRefundList()
    {
        return mOrderRefund::select('*', $this->where);
    }

    public function getOrderGoodsList($orderNos)
    {
        $list = mOrderGoods::select(['sku_id', 'goods_id', 'title', 'cover_img', 'sku_desc'], ['order_no' => $orderNos]);

        if (empty($list)) {
            return [];
        }

        foreach ($list as $info) {
            mOrderGoods::formatInfo($info);
        }

        return $list;
    }

}